package com.jukte.recipe.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import android.util.Log;
 
/**
 * This utility class provides a method that downloads a structure
 * of a directory (excluding files) from a FTP server, using
 * Apache Commons Net API.
 *
 * @author www.codejava.net
 *
 */
public class FTPDownloadUtil {
    /**
     * Download structure of a directory from a FTP server.
     * @param ftpClient an instance of org.apache.commons.net.ftp.FTPClient class.
     * @param parentDir Path of the parent directory of the current directory being
     * downloaded.
     * @param currentDir Path of the current directory being downloaded.
     * @param saveDir path of directory where the whole remote directory will be
     * downloaded and saved.
     * @throws IOException if any network or IO error occurred.
     */
	public FTPFile[] downloadDirectory(FTPClient ftpClient, String parentDir,
	        String currentDir, String saveDir) throws IOException {
	    String dirToList = parentDir;
	    if (!currentDir.equals("")) {
	        dirToList += "/" + currentDir;
	    }
	 
	    FTPFile[] subFiles = ftpClient.listFiles(dirToList);
	    
	    if (subFiles != null && subFiles.length > 0) {
	    	
	        return subFiles;  
	            
	            
	            
	            /*String filePath = parentDir + "/" + currentDir + "/"
	                    + currentFileName;
	            if (currentDir.equals("")) {
	                filePath = parentDir + "/" + currentFileName;
	            }
	 
	            String newDirPath = saveDir + File.separator
	                    + currentDir + File.separator + currentFileName;
	            if (currentDir.equals("")) {
	                newDirPath = saveDir + File.separator
	                          + currentFileName;
	            }
	 
	            if (aFile.isDirectory()) {
	                // create the directory in saveDir
	                File newDir = new File(newDirPath);
	                boolean created = newDir.mkdirs();
	                if (created) {
	                	Log.i("DOWNLOAD", "CREATED the directory: " + newDirPath);
	                } else {
	                	Log.i("DOWNLOAD", "COULD NOT create the directory: " + newDirPath);
	                }
	 
	                // download the sub directory
	                downloadDirectory(ftpClient, dirToList, currentFileName,
	                        saveDir);
	            } else {
	                // download the file
	            	File f = new File(newDirPath);
	            	if(f.exists()){
	            		onDownloadedFile(currentFileName);
	            		continue;
	            	}
	            	URL imageUrl = new URL("http://tahminaghani.com/quinoa/raw/"+currentFileName);
	    			URLConnection ucon = imageUrl.openConnection();

	            	byte buffer[] = new byte[8*1024];
	            	InputStream reader = new BufferedInputStream(ucon.getInputStream());
	                FileOutputStream output= new FileOutputStream(newDirPath);
	                
	                int readByte = 0;
	                synchronized (buffer) {
	                    while ((readByte = reader.read(buffer)) != -1) {
	                    	output.write(buffer,0,readByte);
	                    }
	                    output.flush();
	    				output.close();
	    				reader.close();
	                }
	            	
	                onDownloadedFile(currentFileName);
	                Log.i("DOWNLOAD", "DOWNLOADED the file: " + filePath);
	            }*/
	        /*}*/
	    }
	    return null;
	}
	
	private void downloadFile(){
		
	}
	
	/**
	 * Download a single file from the FTP server
	 * @param ftpClient an instance of org.apache.commons.net.ftp.FTPClient class.
	 * @param remoteFilePath path of the file on the server
	 * @param savePath path of directory where the file will be stored
	 * @return true if the file was downloaded successfully, false otherwise
	 * @throws IOException if any network or IO error occurred.
	 */
	public boolean downloadSingleFile(FTPClient ftpClient,
	        String remoteFilePath, String savePath) throws IOException {
	    File downloadFile = new File(savePath);
	     
	    File parentDir = downloadFile.getParentFile();
	    if (!parentDir.exists()) {
	        parentDir.mkdirs();
	    }
	         
	    OutputStream outputStream = new BufferedOutputStream(
	            new FileOutputStream(downloadFile));
	    try {
	        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
	        return ftpClient.retrieveFile(remoteFilePath, outputStream);
	    } catch (IOException ex) {
	        throw ex;
	    } finally {
	        if (outputStream != null) {
	            outputStream.close();
	        }
	    }
	}
}
